package vn.isolar.task;

import java.io.File;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import vn.isolar.main.SvntestActivity;
import vn.isolar.object.Command;
import vn.isolar.object.Executable;
import vn.isolar.system.audio.Recorder;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import vn.isolar.main.*;
public class TaskCommand extends AsyncTask<Context, Void, String>{
		private Command cmd;
		private Recorder rec;
		private ProgressDialog progressDialog;
		private String result_text = "";
		Context c;
		private File extStore;			// Store sdcard path
		/*
		 * After recording finish, do something here
		 */
		@Override
		protected void onPostExecute(String result_text) {
			//finish recording, disable dialog
			
			//After record, send to server
			cmd = new Command(result_text);
			Executable exe = cmd.createExe();
			if(exe.getType() == "GET_LOCATION"){
				exe.execute(c);
			}
			else
				c.startActivity(exe.createIntent());
		}

		@Override
		protected void onPreExecute() {
			extStore = Environment.getExternalStorageDirectory();
			//rec = new Recorder(); 
		}

		@Override
		protected String doInBackground(Context... arg0) {
			// Uncomment if want to record from microphone
			//String filepath = rec.startRecord();
			//return filepath;
			
			//return extStore.getPath() + "/audio2.wav";
			//return "audio.wav";
			c = arg0[0];
			String filepath=extStore.getPath() + "/audio2.wav";
			try {
				result_text = SvntestActivity.tomClient.execute(filepath, filepath);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// Create command from result
			return result_text;
		}

		
	}